package com.cute.leetcode.editor.cn;

import java.util.*;

public class HJ71字符串通配符 {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            char[] chars1 = sc.nextLine().toLowerCase().toCharArray();
            char[] chars2 = sc.nextLine().toLowerCase().toCharArray();
            // 使用dp
            boolean[][] dp = new boolean[chars1.length + 1][chars2.length + 1];
            dp[0][0] = true;
            for (int i = 1; i <= chars1.length; i++){// 初始是否有连续*存在
                if (chars1[i - 1] == '*') dp[i][0] = true;
                else break;
            }
            for (int i = 1; i <= chars1.length; i++){
                for (int j = 1; j <= chars2.length; j++){
                    if ((chars1[i - 1] == chars2[j - 1]) || (chars1[i - 1] == '?' && valid(chars2[j - 1])))
                        dp[i][j] = dp[i - 1][j - 1];// 当前单个字符可以匹配上，直接看上一位
                    else if(chars1[i - 1] == '*' && valid(chars2[j - 1])){
                        // 单个字符匹配不上，且出现了*，判断是否需要使用*，从三个方向进行判断
                        dp[i][j] = dp[i - 1][j] | dp[i][j - 1] | dp[i - 1][j - 1];
                    }
                }
            }

            System.out.println(dp[chars1.length][chars2.length]);
        }
    }

    public static boolean valid(char c){
        return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z');
    }
}
